import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:fplugin/fplugin.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String _strFromNative = 'Unknown';
  Map<String, dynamic> _jsonFromNative = null;

  @override
  void initState() {
    super.initState();

    //插件需要实例化一下
    Fplugin.init();

    initPlatformState();
  }


  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await Fplugin.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    //如果界面还没有挂载好，则返回了
    if (!mounted) return;
    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _dealClick() async {
    //这里要调用
    String resStr;
    try {
      resStr = await Fplugin.getNativeValue('数据来自flutter');
    } on PlatformException {
      resStr = 'Failed to get native value.';
    }

    //如果界面还没有挂载好，则返回了
    if (!mounted) return;
    setState(() {
      _strFromNative = resStr;
    });
  }

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin test'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                'Running on: $_platformVersion\n',
                style: TextStyle(
                  color: Color(0xFF222222),
                  fontSize: 18
                ),
              ),
              TextButton(
                  onPressed: _dealClick,
                  child: Text(
                    '触发事件',
                    style: TextStyle(
                        color: Color(0xFF222222),
                        fontSize: 18
                    ),
                  )
              ),
              SizedBox(
                height: 25,
              ),
              Text(
                '原生传过来的数据: $_strFromNative\n',
                style: TextStyle(
                    color: Color(0xFF222222),
                    fontSize: 18
                ),
              )
            ],
        ),
      ),
      )
    );
  }
}
